<template>
  <div class="listenlist_container">
    <!-- 顶部搜索 -->
    <div class="listenlist_search">
      <i class="iconfont icon-ico_ximalayaFM" @click="goHome"></i>
      <van-search shape="round" placeholder="请输入搜索关键词" class="van-search" @click="toSearch"/>
    </div>

    <!-- 下拉列表 -->
    <div class="listenlist_navList_content1" v-show="isShow">
      <div class="listenlist_navList_content1_warp">
        <div class="listenlist_navList_up">
          <van-icon
            name="arrow-up"
            color="#f06742"
            size="16"
            @click="pullUpHandler"
          />
        </div>

        <div class="listenlist_navList_check_warp">
          <input
            type="text"
            placeholder="请选择分类"
            class="listenlist_navList_check"
          />
        </div>
        <div class="listenlist_navList_check_bottom_warp">
          <div class="listenlist_navList_check_bottom">
            <span
              class="listenlist_navList_check_bottom_type"
              v-for="(item, index) in navList"
              :key="item.cid"
              @click="changeNavItem(item)"
              >{{ item.title }}</span
            >
          </div>
        </div>
      </div>
     
    </div>
   
   

    <!-- 标签栏滚动 -->
    <div class="listenlist_navList_content">
      <div>
        <div class="listenlist_navList_down" @click="showDropDown">
          <van-icon name="arrow-down" color="#7E8C8D" size="16" />
        </div>
      </div>

      <van-tabs
        v-model:active="active"
        color="#F06742"
        class="listenlist_navList"
        @click-tab="DropDownListHandler"
      >
        <van-tab
          v-for="item in navList"
          :key="item.cid"
          :title="item.title"
          :name="item.name"
        >
          <van-list
            v-model:loading="loading"
            :finished="finished"
            :immediate-check="false"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <div class="listenlist_list_warp">
              <div
                class="listenlist_list"
                v-for="item in dropDownList"
                :key="item.id"
              >
                <div
                  class="listenlist_list_toDetail"
                >
                <!-- <iframe :src="`https://m.ximalaya.com/${item.url}`"></iframe> -->

                  <div class="listenlist_item" @click="toIframe(item.url)">
                    <img
                      :src="item.coverPathSmall"
                      alt=""
                      class="listenlist_list_img"
                    />
                    <div class="listenlist_list_right">
                      <div class="listenlist_list_title">
                        {{ item.title }}
                      </div>
                      <div class="listenlist_list_right_bottom">
                        <span class="list_right_bottom_camera">{{
                          item.dataCount
                        }}</span
                        ><span>{{ item.releaseAt }}更新</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </van-list>
          <van-back-top color="#f06742"/>
        </van-tab>
      </van-tabs>
    </div>
   
   
    

    <!-- 回到顶部 -->
    <!-- <div class="footer">
      <div class="gotop" v-show="gotop" @click="toTop">
        <van-icon name="back-top" color="#fff" />
      </div>
    </div> -->

    
  </div>
</template>

<script lang="ts" setup>
import { useRouter } from "vue-router";
import { ref, onMounted, computed } from "vue";
import listenlistApi, {
  type AnyObject,
  type MoreStatusType,
} from "@/apis/listenlist";
import { formatDate } from "@/utils/formate";


const $router = useRouter();
const loading = ref(false);
const finished = ref(false);
const active = ref("");
const isShow = ref(false);
const showNow = ref(false);
const pageNum = ref(1);
const pageSize = ref(20);
const gotop = ref(false);
const title = ref("youshengshu");
const navList = ref<AnyObject[]>([]);
const dropDownList = ref<AnyObject[]>([]);
const list = [...Array(dropDownList.value).keys()];
const releaseAt = ref(0);
let moreStatus = ref<MoreStatusType>("more");

onMounted(() => {
  getListenlist();
  getDropDownList(title.value);
  // window.addEventListener("scroll", handleScroll, true);
});

// 跳转iframe
const toIframe = (url: string) => {
  
  $router.push({
    path: '/listenlist/iframe',
    query: {
      iframeUrl: url,
    },
  });
}
// 跳转搜索页函数
const toSearch = () => {
  $router.push({ path: "/search" });
};

// 下拉函数
const pullUpHandler = () => {
  isShow.value = false;
};

const showDropDown = () => {
  isShow.value = true;
};

// 回首页
const goHome = () => {
  $router.push({ path: "/home" });
};

// 改变滑动切换
const changeNavItem = (item: AnyObject) => {
  active.value = item.name;
  console.log(item);

  console.log(active.value);

  dropDownList.value = [];
  pageNum.value = 1;
  isShow.value = false;
  getDropDownList(item.name);
};

// 下拉获取列表数据
const DropDownListHandler = (title: AnyObject) => {
  console.log(active.value);
  title.value = title.name;
  dropDownList.value = [];
  pageNum.value = 1;
  getDropDownList(title.value);
};

// 获取听单下的分类列表请求
const getListenlist = async () => {
  try {
    showNow.value = true;
    let res = await listenlistApi.reqListenlist();
    navList.value = res.data.categories;

    showNow.value = false;
  } catch (err) {
    console.error(err);
    showNow.value = false;
  }
};

// 获取某个分类的听单列表请求
const getDropDownList = async (title: string) => {
  try {
    moreStatus.value = "loading";
    let res = await listenlistApi.reqdropDownList(
      title,
      pageNum.value,
      pageSize.value
    );
    let items = res.data.subjects.map((item: any) => {
      return {
        releaseAt: formatDate(new Date(item.releaseAt), "YYYY-MM-DD"),
        url: item.url,
        title: item.title,
        dataCount: item.dataCount,
        coverPathSmall: item.coverPathSmall,
      };
    });
    console.log(items)

    if (items.length > 0) {
      if (items.value < pageSize) {
        moreStatus.value = "noMore";
        finished.value = true;
      } else {
        moreStatus.value = "more";
      }
      dropDownList.value.push(...items);
      pageNum.value++;
      showNow.value = false;
    } else {
      finished.value = true;
    }
  } catch (err) {
    console.error(err);
    showNow.value = false;
    finished.value = true;
  }
};

// 下拉触底
const onLoad = () => {
  setTimeout(() => {
    if ((moreStatus.value = "more")) {
      pageNum.value++;
    }
    if ((moreStatus.value = "more")) {
      getDropDownList(title.value);
    }

    // 加载状态结束
    loading.value = false;

    // 数据全部加载完成
    // if (dropDownList.value.length >= 40) {
    //   finished.value = true;
    // }
  }, 1000);
};

// 回到顶部
// const handleScroll = () => {
//   let scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
//   scrolltop > 200 ? (gotop.value = true) : (gotop.value = false);
// };

// 回到顶部
// const toTop = () => {
//   let top = document.documentElement.scrollTop || document.body.scrollTop;
//   const timeTop = setInterval(() => {
//     document.body.scrollTop = document.documentElement.scrollTop = top -= 50;
//     if (top <= 0) {
//       clearInterval(timeTop);
//     }
//   }, 10);
// };
</script>

<style lang="less" scoped>
/deep/.van-back-top {
  background-color: red;
}
::-webkit-scrollbar {
     width: 0 !important;
   }
::-webkit-scrollbar {
    width: 0 !important;height: 0;
  }
.listenlist_container {
  // 顶部搜索
  .listenlist_search {
    z-index: 1000;
    position: fixed;
    top: 0px;
    background-color: #fff;
    width: 100%;
    height: 50px;
    padding: 15px 15px 5px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .icon-ico_ximalayaFM {
      color: #f06742;
      width: 70px;
      height: 15px;
      margin-right: 3px;
    }

    .van-search {
      width: 300px;
    }
  }

  /deep/.van-tab--active {
    font-size: 18px;
    font-weight: 400;
  }
  // 标签栏滚动
  .listenlist_navList_content {
    position: relative;
    top: 54px;

    .listenlist_navList_down {
      position: absolute;
      z-index: 99;
      right: 0px;
      top: 0px;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 43px;
      height: 43px;
      background-color: #fff;
      border-left: 1px solid #eee;
    }
  }

  // 标签栏滚动点击切换
  .listenlist_navList_content1 {
    .listenlist_navList_content1_warp {
      width: 100%;
      position: absolute;
      height: 42px;
      z-index: 100;
      top: 53px;
    }
    position: relative;

    .listenlist_navList_up {
      position: absolute;
      z-index: 1;
      right: 0px;
      top: 1px;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 43px;
      height: 43px;
      background-color: #fff;
      border-left: 1px solid #eee;
    }
    .listenlist_navList_check_warp {
      height: 43px;
    }
    .listenlist_navList_check {
      height: 43px;
      padding-left: 15px;
      left: 0px;
      width: 100%;
      border: none;
      color: #a3a3ac;
      font-size: 14px;
      border-bottom: 1px solid #eee;
    }

    .listenlist_navList_check_bottom_warp {
      position: absolute;
      z-index: 99;
      height: 300px;
      top: 54px;
      width: 100%;
      border-bottom: 1px solid #eee;
      overflow: auto;
    }
    .listenlist_navList_check_bottom {
      box-sizing: border-box;
      padding-left: 6px;
      padding-bottom: 9px;
      flex-shrink: 0;
      overflow: scroll;
      background-color: #fff;
      -ms-overflow-style: none;
      overflow: -moz-scrollbars-none;
      &::webkit-scrollbar {
        display: none;
      }
      // overflow: hidden;
      .listenlist_navList_check_bottom_type {
        width: 106px;
        height: 36px;
        display: inline-block;
        // padding: 6px 28px;
        margin: 7.5px 7.5px;
        font-size: 16px;
        color: #333;
        border-radius: 4px;
        text-align: center;
        line-height: 36px;
        background-color: #f3f3f5;
        border: none;
      }
    }
  }

  // 列表
  .listenlist_list_warp {
    position: relative;
    width: 100%;
    border-top: 1px solid #eee;
    .listenlist_list {
      width: 100%;
      margin-left: 15px;

      .listenlist_item {
        margin: 15px 0;
        display: flex;
        align-items: center;

        .listenlist_list_toDetail {
          display: block;
        }
        .listenlist_list_img {
          width: 70px;
          height: 70px;
          margin-right: 15px;
        }

        .listenlist_list_right {
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          height: 70px;
          .listenlist_list_title {
            width: 260px;
            font-size: 16px;
            line-height: 1.4;
            color: #333;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
          }
          .listenlist_list_right_bottom {
            font-size: 12px;
            color: #a3a3ac;
          }
          .list_right_bottom_camera {
            margin-right: 14px;
            &:before {
              content: "\e969";
              font-family: "iconfont";
              margin-right: 3px;
            }
          }
        }
      }
    }
  }

  .loading_content {
    width: 100%;
    // height: 100%;
    .loading {
      position: absolute;
      top: 50%;
      left: 50%;
    }
  }

  // 回到顶部
  .footer .gotop {
    text-align: center;
    position: fixed;
    right: 30px;
    bottom: 30px;
    cursor: pointer;
    padding: 10px;
    border-radius: 50%;
    background: #f06742;
  }
}
</style>
